﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio;

using System.ComponentModel.Design;
using Wicresoft.Shell.Data;
using Wicresoft.Shell.Logic;
using Wicresoft.Shell.Core;

namespace Wicresoft.Shell.Package.AppExp
{
    [Guid("43723E33-D911-40ae-B40E-AD0F685C3D9A")]
    public class ConfigurationToolWindow : ToolWindowPane, IVsWindowFrameNotify2
    {
        private MyConfigurationControl control;
        public string treeNodeID;
        public ConfigurationToolWindow() :
            base(null)
        {
            // Set the window title reading it from the resources.
            this.Caption = Resources.ConfigurationToolWindowTitle;
            // Set the image that will appear on the tab of the window frame
            // when docked with an other window
            // The resource ID correspond to the one defined in the resx file
            // while the Index is the offset in the bitmap strip. Each image in
            // the strip being 16x16.
            this.BitmapResourceID = 301;
            this.BitmapIndex = 1;
         
            control = new MyConfigurationControl(this);
            this.treeNodeID = control.treeNodeID;

            this.ToolBar = new CommandID(GuidList.guidAppExpCmdSet, PkgCmdIDList.cmdidConfigurationToolBarMenu);
            OleMenuCommandService mcs = GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
            if (null != mcs)
            {
                CommandID saveButton = new CommandID(GuidList.guidAppExpCmdSet, PkgCmdIDList.cmdidConfigurationButtonSave);
                MenuCommand saveCommand = new MenuCommand(new EventHandler(SaveButton_Click), saveButton);
                mcs.AddCommand(saveCommand);
            }
        }

        private void SaveButton_Click(object sender, EventArgs e)
        {
            control.UpdateData();
        }

        override public IWin32Window Window
        {
            get
            {
                return (IWin32Window)control;
            }
        }

        #region #region IVsWindowFrameNotify2 Members

        public int OnDockableChange(int fDockable)
        {
            return VSConstants.S_OK;
        }

        public int OnMove()
        {
            return VSConstants.S_OK;
        }

        public int OnShow(int fShow)
        {
            if (fShow == (int)__FRAMESHOW.FRAMESHOW_WinClosed)
            {
            }
            return VSConstants.S_OK;
        }

        public int OnSize()
        {
            return VSConstants.S_OK;
        }

        public int OnClose(ref uint pgrfSaveOptions)
        {
            if (control.DataGridViewChanged)
            {
                DialogResult result = MessageBox.Show(Resources.UnsavedWarning, Resources.WarningCaption, MessageBoxButtons.YesNoCancel);
                if (result == DialogResult.Yes)
                {
                    control.UpdateData();
                }
                else if (result == DialogResult.Cancel)
                {
                    return VSConstants.E_ABORT;
                }
            }

            return VSConstants.S_OK;
        }

        #endregion
    }
}
